home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Libraries
/
Sherlock 2.0
/
DevLibSrc
/
Main_DevLib
/
LIBlist.h
< prev
next >
Wrap
Text File
|
1995-05-27
|
3KB
|
172 lines
/*
devlib: header file containing list macros.
source: LIBlist.h
started: December 13, 1993.
version:
May 27, 1995.
Added lst2_free_all_macro.
March 17, 1994.
Added lst_general_reverse_macro.
Wrote lst_reverse_macro in terms of lst_general_reverse_macro.
March 6, 1994.
Bug fix: rewrote lst_reverse_macro.
*/
#ifndef LIBlist_h_
#define LIBlist_h_
#pragma once
#define TYPE_LIST(type)\
type * next /* Pointer to next object. */
#define TYPE_LIST2(type)\
type * next2; /* Pointer to next object. */ \
type * back2 /* Pointer to previous object. */
/* Add a node to the beginning of a list. */
#define lst_add_macro(elem, list)\
{\
ASSERT(elem);\
elem -> next = list;\
list = elem;\
}
#define lst2_add_macro(elem, list)\
{\
ASSERT(elem);\
if (list) {\
list -> back2 = elem;\
}\
elem -> next2 = list;\
elem -> back2 = NULL;\
list = elem;\
}
/* Append a node to the end of a list. */
#define lst_append_macro(elem, list, type)\
{\
ASSERT(elem);\
elem -> next = NULL;\
if (list == NULL) {\
list = elem;\
}\
else {\
type * np_ = list;\
while(np_ -> next) {\
np_ = np_ -> next;\
}\
np_ -> next = elem;\
}\
}
#define lst2_append_macro(elem, list, type)\
{\
ASSERT(elem);\
elem -> next2 = NULL;\
if (list == NULL) {\
list = elem;\
elem -> back2 = NULL;\
}\
else {\
type * np_ = list;\
while(np_ -> next2) {\
np_ = np_ -> next2;\
}\
np_ -> next2 = elem;\
elem -> back2 = np_;\
}\
}
/*
Traverse a list.
The "safe" versions work even if node p is deleted.
This macro is an anachronism.
*/
#define lst_every_macro(p,list)\
for(p=list; p; p=p->next)
/* Free all nodes on a list. */
#define lst_free_all_macro(list, type)\
{\
type * np_ = list;\
type * np2_;\
while(np_) {\
np2_ = np_ -> next;\
obj_free_macro(np_);\
np_ = np2_;\
}\
list = NULL;\
}
/* Added 5/27/95. */
#define lst2_free_all_macro(list, type)\
{\
type * np_ = list;\
type * np2_;\
while(np_) {\
np2_ = np_ -> next2;\
obj_free_macro(np_);\
np_ = np2_;\
}\
list = NULL;\
}
/* Remove a node from a possibly empty list. */
#define lst_remove_macro(elem, list, type)\
{\
type * np_ = list;\
type * np2_ = NULL;\
ASSERT(elem);\
if (list && elem && list == elem) {\
list = elem -> next;\
}\
else if (list && elem) {\
for(np2_ = np_ -> next; np2_; np_ = np2_, np2_ = np_ -> next) {\
if (np2_ == elem) {\
np_ -> next = np2_ -> next;\
break;\
}\
}\
}\
}
#define lst2_remove_macro(elem, list)\
{\
ASSERT(elem);\
if (elem -> back2 == NULL) {\
list = elem -> next2;\
}\
else {\
elem -> back2 -> next2 = elem -> next2;\
}\
if (elem -> next2) {\
elem -> next2 -> back2 = elem -> back2;\
}\
}
#define lst_reverse_macro(the_list, the_type) \
lst_general_reverse_macro(the_list, next, the_type)
#define lst_general_reverse_macro(the_list, the_next_field, the_type)\
{\
register the_type * new_p = NULL;\
register the_type * old_p = NULL;\
register the_type * next_p = NULL;\
\
for (old_p = (the_list); old_p; old_p = next_p) {\
next_p = old_p -> the_next_field;\
old_p -> the_next_field = new_p;\
new_p = old_p;\
}\
(the_list) = new_p;\
}
#endif /* LIBlist_h_ */